package org.example;

import java.util.*;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
//
//        Map<String,Integer> map = new TreeMap();
//        map.put("apple",3);
//        map.put("beer",4);
//
//        map.getOrDefault("banana",0);
//
//        Set<String> ret = map.keySet();
//        Collection<Integer> values = map.values();
//        System.out.println(values);
//
//        Set<Map.Entry<String, Integer>> entries = map.entrySet();
//        for(Map.Entry<String,Integer> entry : entries){
//            System.out.println("key: "+entry.getKey() + "  value: " + entry.getValue());
//        }
//
//        Map<Student,Integer> m1 = new TreeMap<>((Student o1, Student o2)->{
//            return o1.age-o2.age;
//        });
//        m1.put(new Student(22),1);
//        m1.put(new Student(31),2);
//        m1.put(new Student(53),5);
//        m1.put(new Student(33),3);
//        m1.put(new Student(44),4);
//        System.out.println(m1);
        Map<String, Integer> map = new HashMap<>();
        Integer apple = map.put("apple", 1);
        map.put("apple2", 2);
        map.put("apple3", 3);
        map.put("apple4", 4);
        System.out.println(apple);
//      int a = map.put("apple",2);
//      System.out.println(a);
        //map中不允许存在相同的key进行映射，如果存在相同的key进行映射会发生新元素V（value）覆盖旧元素的值V
//      String string = map.put("理综", "300");
        //相同的k存放新元素会覆盖掉旧元素
        Integer apple1 = map.get("apple");
        System.out.println(apple1);
        int size = map.size();
        System.out.println(size);
        System.out.println(map.remove("apple"));
        System.out.println(map.remove("apple2"));
        System.out.println(map.size());
        boolean empty = map.isEmpty();
        System.out.println(empty);
        System.out.println("+++++++++++++++++++++++++++++++++++");
        System.out.println(map.containsKey("apple"));
        System.out.println(map.containsValue("100"));


    }

    class Student {
        int age;
        String name;

        public Student(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }

    }
}
class MapTest{

    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();

        map.put("apple",1);
        map.put("banana",2);
        map.put("starwberry",3);
        map.put("beer",4);
        System.out.println(map);

        /**
         * 遍历所有的key
         * Set<K> keySet()
         * 将当前map中所有的key以一个Set集合返回
         * Set不可重复，天然降重
         */
        Set<String> keySet = map.keySet();
        for (String k : keySet){
            System.out.println(k);
        }
        System.out.println("????????????????????????????????");

        Collection<Integer> values = map.values();
        for (Integer value : values){
            System.out.println(value);
        }

        Set<Entry<String, Integer>> entries = map.entrySet();
        for (Entry<String,Integer> entry : entries){
            entry.getKey();
            entry.getValue();
            System.out.println(entry);
        }
        System.out.println("++++++++++++======================================");

        Iterator<Integer> it1 = values.iterator();
        while(it1.hasNext()){
            Integer next = it1.next();
            System.out.println(next);
        }

        Iterator<String> it2 = keySet.iterator();
        while(it2.hasNext()){
            String s = it2.next();
            System.out.println(s);
        }

        Iterator<Entry<String, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("=====================================================");

        values.forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer val) {
                System.out.println(val);
            }
        });

        values.forEach((Integer val)->{
            System.out.println(val);
        });

        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");

        keySet.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        

        keySet.forEach((String s)->{
            System.out.println(s);
        });
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++");

        entries.forEach(new Consumer<Entry<String, Integer>>() {
            @Override
            public void accept(Entry<String, Integer> stringIntegerEntry) {
                System.out.println(stringIntegerEntry);
            }
        });
    }
}

